package iuiga.enable;

import iuiga.thirdparty.message.strategy.MessageAbstract;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.ClassMetadata;

import java.util.Optional;

/**
 * 自定义资源加载器
 */
public class ClassPathResourceScanner extends ClassPathBeanDefinitionScanner {

    private static final String RESOURCE_COMPONENT=ThirdpartyBeanName.class.getName();

    public ClassPathResourceScanner(BeanDefinitionRegistry registry) {
        super(registry,false);
    }

    public void registerFilters() {
        boolean acceptAllInterfaces = true;
        if (acceptAllInterfaces) {
            addIncludeFilter((metadataReader, metadataReaderFactory) -> {
                return metadataReader.getAnnotationMetadata().hasAnnotation(RESOURCE_COMPONENT);
            });
        }
        //排除package-info.java
        addExcludeFilter((metadataReader, metadataReaderFactory) -> {
            String className = metadataReader.getClassMetadata().getClassName();
            return className.endsWith("package-info");
        });
    }

    //iuiga.strategy包下所有继承了MessageAbstract的类都是bean组件
    @Override
    protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition){
        return true;
    }


}
